package com.osce.score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.score.ScoreExExam;
import com.csce.domain.entity.score.ScoreExExamStudent;
import com.doudou.core.container.ListUtil;
import com.doudou.core.lang.ObjUtil;
import com.doudou.core.throwable.GlobalException;
import com.osce.common.constant.CacheConstants;
import com.osce.common.core.redis.RedisCache;
import com.osce.interfaces.score.IScoreExExamService;
import com.osce.interfaces.score.IScoreExExamStudentService;
import com.osce.score.mapper.ScoreExExamStudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 考试学员Service业务层处理
 *
 * @author zzx
 * @since 2023-09-05
 */
@Service
public class ScoreExExamStudentServiceImpl extends ServiceImpl<ScoreExExamStudentMapper, ScoreExExamStudent> implements IScoreExExamStudentService {

    @Resource private IScoreExExamService examService;
    @Resource private RedisCache redisCache;

    /**
     * 查询考试学员
     *
     * @param id 考试学员主键
     * @return 考试学员
     */
    @Override
    public ScoreExExamStudent selectScoreExExamStudentById(Long id) {
        return baseMapper.selectScoreExExamStudentById(id);
    }

    /**
     * 查询考试学员列表
     *
     * @param scoreExExamStudent 考试学员
     * @return 考试学员
     */
    @Override
    public List<ScoreExExamStudent> selectScoreExExamStudentList(ScoreExExamStudent scoreExExamStudent) {
        return baseMapper.selectScoreExExamStudentList(scoreExExamStudent);
    }

    /**
     * 新增考试学员
     *
     * @param scoreExExamStudent 考试学员
     * @return 结果
     */
    @Override
    public int insertScoreExExamStudent(ScoreExExamStudent scoreExExamStudent) {
        return baseMapper.insertScoreExExamStudent(scoreExExamStudent);
    }

    /**
     * 修改考试学员
     *
     * @param scoreExExamStudent 考试学员
     * @return 结果
     */
    @Override
    public int updateScoreExExamStudent(ScoreExExamStudent scoreExExamStudent) {
        return baseMapper.updateScoreExExamStudent(scoreExExamStudent);
    }

    /**
     * 批量删除考试学员
     *
     * @param ids 需要删除的考试学员主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamStudentByIds(Long[] ids) {
        return baseMapper.deleteScoreExExamStudentByIds(ids);
    }

    /**
     * 删除考试学员信息
     *
     * @param id 考试学员主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamStudentById(Long id) {
        return baseMapper.deleteScoreExExamStudentById(id);
    }

    @Override
    public List<Long> selectScoreExExamStudentCheckIds(Long examId) {
        return baseMapper.selectScoreExExamStudentCheckIds(examId);
    }

    @Override
    public int batchInsertExExamStudent(Long examId, List<Long> userIdList) {
        if (ObjUtil.isEmpty(userIdList)) {
            return 0;
        }
        //查询考试
        ScoreExExam exam = examService.selectScoreExExamById(examId);
        //查询已添加的学员
        List<Long> checkIds = baseMapper.selectScoreExExamStudentCheckIds(examId);
        if (userIdList.size() + checkIds.size() > exam.getMaxStudent()) {//判断是否超员
            throw new GlobalException("添加的学员已超出最大学员数");
        }
        List<ScoreExExamStudent> students = ListUtil.linkedList();
        for (Long userId : userIdList) {
            students.add(ScoreExExamStudent.builder().examId(examId).userId(userId).build());
        }
        int i = baseMapper.batchInsertExExamStudent(students);
        if (i > 0){
            updateCache(examId);
        }
        return i;
    }


    @Override
    public int batchDeleteScoreExExamStudentByIds(ScoreExExamStudent scoreExExamStudent) {
        if (ObjUtil.isEmpty(scoreExExamStudent) && ObjUtil.isEmpty(scoreExExamStudent.getUserIdList())) {
            return 0;
        }
        int i = baseMapper.batchDeleteScoreExExamStudentByIds(scoreExExamStudent.getExamId(), scoreExExamStudent.getUserIdList());
        if (i > 0){
            updateCache(scoreExExamStudent.getExamId());
        }
        return i;
    }

    @Override
    public int deleteScoreExExamStudentByExamId(Long examId) {
        return baseMapper.deleteScoreExExamStudentByExamId(examId);
    }

    @Override
    public int deleteScoreExExamStudentByExamIds(Long[] examIds) {
        return baseMapper.deleteScoreExExamStudentByExamIds(examIds);
    }

    public void updateCache(Long examId){
        String key = CacheConstants.SCORE_EXAM_STUDENT_IDS_KEY + examId;
        redisCache.deleteObject(key);
        //查询已添加的学员
        List<Long> longs = baseMapper.selectScoreExExamStudentCheckIds(examId);
        if (ListUtil.isNotEmpty(longs)){
            redisCache.setCacheList(key, longs);
        }
    }

    @Override
    public List<Long> getCacheIdsByExamId(Long examId){
        return redisCache.getCacheList(CacheConstants.SCORE_EXAM_STUDENT_IDS_KEY + examId);
    }
}
